package com.mingtu.hikvideo2.utils;

import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

public class RecordTimeUtils {
    private static String TAG = "TimeUtils";
    private Timer mTimer = null;
    private TimerTask mTimerTask = null;
    private Handler mHandler = null;
    private static long count = 0;
    private boolean isPause = false;
    private static int delay = 1000;  //1s
    private static int period = 1000;  //1s
    private static final int UPDATE_TEXTVIEW = 0;
    TextView mTextView;
//    public static TimeUtils timeUtils;
//    public static TimeUtils getInstance() {
//        synchronized (TimeUtils.class) {
//            if (timeUtils == null) {
//                timeUtils = new TimeUtils();
//            }
//            return timeUtils;
//        }
//    }


    public RecordTimeUtils(TextView mTextView) {
        this.mTextView = mTextView;
        mHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case UPDATE_TEXTVIEW:
                        updateTextView();
                        break;
                    default:
                        break;
                }
            }
        };
    }

    public void puseTimer() {
        isPause = !isPause;
    }

    private void updateTextView() {
        CharSequence sysTimeStr = formatSeconds4(getTime());
        mTextView.setText(String.valueOf(sysTimeStr));
    }

    public void startTimer() {

        if (mTimer == null) {
            mTimer = new Timer();
        }

        if (mTimerTask == null) {
            mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    sendMessage(UPDATE_TEXTVIEW);

                    count++;
                    setTime(count);
                }
            };
        }

        if (mTimer != null && mTimerTask != null)

            mTimer.scheduleAtFixedRate(mTimerTask, delay, period);

    }

    public void stopTimer() {

        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }

        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }

    public void sendMessage(int id) {
        if (mHandler != null) {
            Message message = Message.obtain(mHandler, id);
            mHandler.sendMessage(message);
        }
    }

    public long getTime() {
        return count;
    }

    public void setTime(long count) {
        this.count = count;
    }

    /**
     * 格式化秒数
     *
     * @param seconds
     * @return
     */
    public static String formatSeconds4(long seconds) {
        String standardTime;
        if (seconds <= 0) {
            standardTime = "00:00";
        } else if (seconds < 60) {
            standardTime = String.format(Locale.getDefault(), " 00:%02d", seconds % 60);
        } else if (seconds < 3600) {
            standardTime = String.format(Locale.getDefault(), "%02d:%02d", seconds / 60, seconds % 60);
        } else {
            standardTime = String.format(Locale.getDefault(), "%02d:%02d:%02d", seconds / 3600, seconds % 3600 / 60, seconds % 60);
        }
        return standardTime;
    }

}